package util 

import(
	"time"
)

const (
	//NormalDate yyyy-MM-dd
	NormalDate = "2006-01-02"

	// CompactDateTime yyyyMMddHHmmss
	CompactDateTime = "20060102150405"

	//NormalDateTime yyyy-MM-ddTHH:mm:ss
	NormalDateTime = "2006-01-02T15:04:05"

	//NormalDateTime2 yyyy-MM-dd HH:mm:ss

	// LayoutSecond yyyy-MM-dd HH:mm
	LayoutMinute = "2006-01-02 15:04"

	// LayoutSecond yyyy-MM-dd HH:mm:ss
	LayoutSecond = "2006-01-02 15:04:05"

	// LayoutSecondWithTimeZone yyyy-MM-dd HH:mm:ss +0800
	LayoutSecondWithTimeZone = "2006-01-02 15:04:05 -0700"

	// LayoutSecondWithTimeZoneAndT yyyy-MM-ddTHH:mm:ss0800
	LayoutSecondWithTimeZoneAndT = "2006-01-02T15:04:05-07:00"
)

// GoTimeToString time.Time => string yyyy-MM-ddTHH:mm:ss(+0800)
func GoTimeToString (t time.Time) string {
	return t.Format(LayoutSecond)
}

// GoDateToString time.Time => string yyyy-MM-dd
func GoDateToString (t time.Time) string {
	return t.Format(NormalDate)
}

// GoTimeToStringf time.Time => string yyyy-MM-ddTHH:mm:ss
func GoTimeToStringf (t time.Time, layout string) string {
	return t.Format(layout)
}

// StringToGoTime string => time.Time
func StringToGoTime(timeStr string) (time.Time, error) {
	var r time.Time
	if timeStr != "" {
		t, err := time.Parse(LayoutSecond, timeStr)
		if err != nil {
			return r, err
		}
		r = t
	}
	return r, nil
}

// StringfToGoTime string => time.Time
func StringfToGoTime(timeStr string, layout string) (time.Time, error) {
	var r time.Time
	if timeStr != "" {
		t, err := time.Parse(layout, timeStr)
		if err != nil {
			return r, err
		}
		r = t
	}
	return r, nil
}


// MsToGoTime unix millisecond => time.Time
func MsToGoTime(ms int64) (time.Time) {
	return time.Unix(0, ms*int64(time.Millisecond))
}

// IsAfterNow t is after now()
func IsAfterNow (t time.Time) bool {
	return t.After(time.Now())
}

// IsBeforeNow t is before now()
func IsBeforeNow (t time.Time) bool {
	return t.Before(time.Now())
}

// Uint8ToSecondDuration a
func Uint8ToSecondDuration (v uint8) time.Duration {
	return time.Duration(v) * time.Second
}